package co.fitstart.admin.web.controller.trade.coupon;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.support.Page;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.resolver.Preloading;
import com.demo2do.core.web.utils.WebUtils;

import co.fitstart.admin.entity.content.CreditCouponBatchSearch;
import co.fitstart.admin.service.CouponService;
import co.fitstart.admin.service.CreditCouponBatchService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.entity.coupon.Coupon;
import co.fitstart.entity.coupon.CouponBatch;
import co.fitstart.entity.coupon.CreditCouponBatch;
import co.fitstart.entity.coupon.ExpireType;

/**
 * 
 * @author Ysy
 *
 */
@Controller
@RequestMapping("/credit-coupon-batches")
@MenuSetting("menu-credit")
public class CreditCouponBatchController {
    
    @Autowired
    private CreditCouponBatchService creditCouponBatchService;
    
    @Autowired
    private CouponService<CouponBatch> couponBatchService;
    
    @Autowired
    private CouponService<Coupon> couponService;
    
    /**
     * List all credit coupon batches
     * 
     * @param request
     * @param page
     * @return
     */
    @RequestMapping("")
    @MenuSetting("submenu-credit-coupon-batch-list")
    public ModelAndView list(HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/coupon/credit-batch/credit-coupon-batch-list");
        
        List<CreditCouponBatch> creditCouponBatchs = creditCouponBatchService.list(page);
        
        modelAndView.addObject("creditCouponBatchs", creditCouponBatchs);
        modelAndView.addObject("refer", "list");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
        
        return modelAndView;
    }
    
    /**
     * Display the search panel
     * 
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public ModelAndView search() {
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/coupon/credit-batch/credit-coupon-batch-search");
        
        return modelAndView;
    }
    
    /**
     * Do search
     * 
     * @param creditCouponBatchSearch 
     * @param request
     * @param page
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET, params = "filter")
    @MenuSetting("submenu-credit-coupon-batch-list")
    public ModelAndView onSearch(CreditCouponBatchSearch creditCouponBatchSearch, HttpServletRequest request, Page page) {
        
        ModelAndView modelAndView  = new ModelAndView("ecommerce/coupon/credit-batch/credit-coupon-batch-list");
        
        List<CreditCouponBatch> creditCouponBatchs = creditCouponBatchService.search(creditCouponBatchSearch, page);
        
        modelAndView.addObject("creditCouponBatchs", creditCouponBatchs);
        modelAndView.addObject("refer", "search");
        modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page", "filter"));
        
        return modelAndView;
        
    }
    
    /**
     * View
     * 
     * @param id
     * @param page 
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @MenuSetting("submenu-credit-coupon-batch-list")
    public ModelAndView view(@PathVariable Long id, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/coupon/credit-batch/credit-coupon-batch-view");
        
        CreditCouponBatch creditCouponBatch = creditCouponBatchService.load(CreditCouponBatch.class, id);
        
        List<Coupon> coupons = couponBatchService.listCoupons(creditCouponBatch.getCouponBatch(), page);
        
        modelAndView.addObject("coupons", coupons);
        modelAndView.addObject("creditCouponBatch", creditCouponBatch);
        modelAndView.addObject("page", page);
        
        return modelAndView;
    }
    
    /**
     * Display create panel
     * 
     * @return
     */
    @RequestMapping(value = "/create",  method = RequestMethod.GET)
    @MenuSetting("submenu-credit-coupon-batch-list")
    public ModelAndView create() {
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/coupon/credit-batch/credit-coupon-batch-create");
        
        return modelAndView;
    }
    
    /**
     * Do create 
     * @param creditCouponBatch 
     * 
     * @return
     */
    @RequestMapping( value = "", method = RequestMethod.POST)
    public @ResponseBody String onCreate(CreditCouponBatch creditCouponBatch) {
        
        Result result = new Result();
        
        try {
            
            creditCouponBatchService.create(creditCouponBatch);
            result.success().message("新️建积分优惠券批次成功！");
        
        } catch(Exception e) {
            
            result.fail().message("新️建积分优惠券批次失败！");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/generate-coupon", method = RequestMethod.GET)
    public ModelAndView generateCoupon(@PathVariable Long id){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/coupon/credit-batch/credit-coupon-generate");
        
        modelAndView.addObject("id", id);
        return modelAndView;
        
    }
    
    /**
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/generate-coupon", method = RequestMethod.PATCH)
    public @ResponseBody String onGenerateCoupon(@PathVariable("id") Long id){
        
        Result result = new Result();
        
        try {
            
            CreditCouponBatch creditCouponBatch = creditCouponBatchService.load(CreditCouponBatch.class, id);
            creditCouponBatchService.generateCoupon(creditCouponBatch);
            result.success().message("预生成券成功！");

        } catch (Exception e) {

            result.fail().message("预生成券失败！");

        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * Display edit panel
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    @MenuSetting("submenu-credit-coupon-batch-list")
    public ModelAndView editInfo(@PathVariable Long id) {
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/coupon/credit-batch/credit-coupon-batch-edit");
        
        CreditCouponBatch creditCouponBatch = creditCouponBatchService.load(CreditCouponBatch.class, id);
        
        modelAndView.addObject("creditCouponBatch", creditCouponBatch);
        
        return modelAndView;
        
    }
    
    /**
     * Do edit
     * 
     * @param creditCouponBatch 
     * @param expireType
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
    public @ResponseBody String onEditInfo(@Preloading("creditBatchId") CreditCouponBatch creditCouponBatch, @RequestParam("inputExpireType") ExpireType expireType) {

        Result result = new Result();
        
        try {
            creditCouponBatchService.edit(creditCouponBatch, expireType);
            result.success().message("修改批次信息成功！");

        } catch (Exception e) {

            result.fail().message("修改批次信息失败！");

        }
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * display edit description panel
     * @param id 
     * 
     * @return
     */
    @RequestMapping(value = "/{id}/description/edit", method = RequestMethod.GET)
    @MenuSetting("submenu-credit-coupon-batch-list")
    public ModelAndView editDescription(@PathVariable("id")Long id){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/coupon/credit-batch/credit-coupon-batch-description-edit");
        
        modelAndView.addObject("id", id);
        return modelAndView;
    }
    
    /**
     * read description
     * 
     * @param id
     * @return
     */
    @RequestMapping("/{id}/description")
    public @ResponseBody String readDescription(@PathVariable("id")Long id){
        
        CreditCouponBatch creditCouponBatch = creditCouponBatchService.load(CreditCouponBatch.class, id);
        return creditCouponBatch.getDescription();
    }
    
    /**
     * do edit description
     * @param id 
     * @param description 
     * 
     * @return
     */
    @RequestMapping(value = "/{id}/description", method = RequestMethod.PATCH)
    public @ResponseBody String onEditDescription(@PathVariable("id")Long id, @RequestParam("description")String description){
        Result result = new Result();
        
        try{
            CreditCouponBatch creditCouponBatch = creditCouponBatchService.load(CreditCouponBatch.class, id);
            creditCouponBatchService.update(creditCouponBatch.editDescription(description));
            
            result.success().message("修改积分券批次详情成功");
            
        }catch(Exception e){
            
            result.fail().message("修改积分券批次详情失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * Display edit cover panel
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/cover", method = RequestMethod.GET)
    @MenuSetting("submenu-credit-coupon-batch-list")
    public ModelAndView editCover(@PathVariable Long id) {
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/coupon/credit-batch/credit-coupon-batch-cover-edit");
        
        CreditCouponBatch creditCouponBatch = creditCouponBatchService.load(CreditCouponBatch.class, id);
        
        modelAndView.addObject("creditCouponBatch", creditCouponBatch);
        
        return modelAndView;
        
    }
    
    /**
     * Do edit cover
     * 
     * @param id 
     * @param cover 
     * 
     * @return
     */
    @RequestMapping(value = "/{id}/cover", method = RequestMethod.PATCH)
    public @ResponseBody String onEditCover(@PathVariable("id") Long id, String cover) {

        Result result = new Result();
        
        try {
            
            CreditCouponBatch creditCouponBatch = creditCouponBatchService.load(CreditCouponBatch.class, id);
            
            creditCouponBatchService.update(creditCouponBatch.editCover(cover));
            
            result.success().message("修改批次图片成功！");

        } catch (Exception e) {

            result.fail().message("修改批次图片失败！");

        }
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * 
     * @param id 
     * @param couponId
     * @return
     */
    @RequestMapping(value = "/{id}/coupons/{couponId}/identify", method = RequestMethod.GET)
    public ModelAndView identifyCoupon(@PathVariable("id") Long id,
                                       @PathVariable("couponId") Long couponId){
        
        ModelAndView modelAndView = new ModelAndView("ecommerce/coupon/credit-batch/credit-coupon-identify");
        
        Coupon coupon = couponService.load(Coupon.class, couponId);
        
        modelAndView.addObject("coupon", coupon);
        modelAndView.addObject("id", id);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param couponId
     * @param identification
     * @return
     */
    @RequestMapping(value = "/{id}/coupons/{couponId}/identify", method = RequestMethod.PATCH)
    public @ResponseBody String onIdentifyCoupon(@PathVariable("couponId") Long couponId,
                                                 @RequestParam("identification") String identification){
        
        Result result = new Result();
        
        try {
            
            Coupon coupon = couponService.load(Coupon.class, couponId);
            creditCouponBatchService.identify(coupon, identification);
            
            result.success().message("优惠券指定用户成功！");

        } catch (Exception e) {

            result.fail().message("优惠券指定用户失败！");

        }
        return JsonUtils.toJsonString(result);
    }
    
}
